<?php
    include_once "for_include/check_user.php";
    
    header("Content-Type:application/json");
    
    $username = isset($_GET['username'])?$_GET['username']:null;
    
    $sql_config=include "server_config/config.php";
    $dbobj = new mysqli($sql_config['db_host'],$sql_config['db_user'],$sql_config['db_pw'],$sql_config['db_name'],$sql_config['db_port']);

    $playerInfo = getPlayerInfo($username);
    if($playerInfo)
    {
        echo json_encode($playerInfo,JSON_PRETTY_PRINT);
    }
    else
    {
        http_response_code(404);
        echo json_encode(["error"=>"玩家未找到"]);
    }
    //逻辑改了 如果没找到这个玩家 就从1到7分配一个steve 这个是在getPlayerInfo()实现的


    // if($username)
    // {
    //     $playerInfo = getPlayerInfo($username);
    //     if($playerInfo)
    //     {
    //         echo json_encode($playerInfo,JSON_PRETTY_PRINT);
    //     }
    //     else
    //     {
    //         http_response_code(404);
    //         echo json_encode(["error"=>"玩家未找到"]);
    //     }
    // }
    // else
    // {
    //     http_response_code(400);
    //     echo json_encode(["error"=>"缺少用户名参数"]);
    // }

    function getPlayerInfo($username)
    {
        $skin_json=[];

        if(!check_user($username)) //如果这个玩家没有在我的皮肤服务器创建账号 就从1到7分配一个随机Steve
        {
            $rand_skin=rand(1,7);
            $skin_json=[
                "username"=>$username,
                "skins"=>[
                    "default"=>$rand_skin,
                    "slim"=>$rand_skin,
                    
                ],
                "cape"=>"",
                "elytra"=>""
            ];
        }
        else //如果有账号
        {
            global $dbobj;

            

            $stmt=$dbobj->prepare("select * from mc_skin_con where username = ?");

            
            $stmt->bind_param("s",$username);
            
            $stmt->execute();
            
            $stmt->bind_result($id,$name,$changeCode,$skins_default,$skins_slim,$skins_cape,$skins_elytra);
            $stmt->fetch();

            //如果有账号 但是没有default和slim的皮肤 就随机分配一个Steve
            $rand_skin=rand(1,7); 
            if(!$skins_default && !$skins_slim) $skins_default=$rand_skin;


            $skin_json=[
                "username"=>$username,
                "skins"=>[
                    "default"=>$skins_default,
                    "slim"=>$skins_slim,
                    
                ],
                "cape"=>$skins_cape,
                "elytra"=>$skins_elytra
            ];


        }

        return isset($username)?$skin_json:null;
    }

?>